<?php
class SubscribersController extends AppController {

	var $name = 'Subscribers';
	var $helpers = array('Html', 'Form');
	
	function beforeFilter() {
		parent::beforeFilter(); 
		/*$this->Auth->deny('index', 'view', 'delete', 'add', 'edit');
		$this->Auth->allow('register', 'remove');*/
	}

	
	/* Todo: Send register mail */
	function register() {
		$message = __('Enter your email to add yourself to the subscriber list.', true);
		$render_status = false;
		if (!empty($this->data)) {
			$subscriber = $this->Subscriber->findByEmail($this->data['Subscriber']['email']);
			if(empty($subscriber)) {
				$this->Subscriber->create();
				if ($this->Subscriber->save($this->data)) {
					$message = __('Your email has been saved.', true);
					$render_status = true;
				} else {
					$message = __('Your email could not be saved. Please, try again.', true);
				}
			} else {
				if($subscriber['Subscriber']['deleted'] == 1) {
					$this->Subscriber->id = $subscriber['Subscriber']['id'];
					if($this->Subscriber->saveField('deleted', 0)) {
						$message = __('Your email has been saved.', true);
						$render_status = true;
					} else {
						$message = __('Your email could not be saved. Please, try again.', true);
					}
				} else {
					$message = __('Your email is already registered in subscriber list.', true);
				}
			}
			
		}
		
		$this->Session->setFlash($message);
		$this->set('message', $message);
		if($render_status) {
			$this->render('status');
		}
	}
	
	/* Todo: Send register mail */
	function remove() {
		$message = __('Enter your email to remove yourself from the subscriber list.', true);
		if (!empty($this->data)) {
			$subscriber = $this->Subscriber->findByEmail($this->data['Subscriber']['email']);
			//debug($subscriber);
			if(empty($subscriber)) {
				$message = __('That email was not found in the subscriber list.', true);
			} else {
				if($subscriber['Subscriber']['deleted'] == 1) {
					$message = __('That email was not found in the subscriber list.', true);
				} else {
					$this->Subscriber->id = $subscriber['Subscriber']['id'];
					if($this->Subscriber->saveField('deleted', 1)) {
						$message = __('The following email was removed from subscriber list: ', true) . $subscriber['Subscriber']['email'];
					}
					
				}
				
			}
		}
		$this->Session->setFlash($message);
		$this->set('message', $message);
	}
	
	function admin_index() {
		$this->Subscriber->recursive = 0;
		$this->set('subscribers', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Subscriber.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('subscriber', $this->Subscriber->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Subscriber->create();
			if ($this->Subscriber->save($this->data)) {
				$this->Session->setFlash(__('The Subscriber has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Subscriber could not be saved. Please, try again.', true));
			}
		}
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Subscriber', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Subscriber->save($this->data)) {
				$this->Session->setFlash(__('The Subscriber has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Subscriber could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Subscriber->read(null, $id);
		}
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Subscriber', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Subscriber->del($id)) {
			$this->Session->setFlash(__('Subscriber deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>